# -*- coding: UTF-8 -*-
import pandas as pd
import time,os,datetime,math,struct,sys
from colorama import init, Fore, Back, Style
# from makedir import FileOp
pd.set_option('display.expand_frame_repr', False)
init(autoreset=True)
import logging
from loguru import logger
from struct import *
# from tqdm import tqdm
currentDate = time.strftime("%Y%m%d", time.localtime())
LOG_DIR = os.path.expanduser("logs/")
LOG_FILE = os.path.join(LOG_DIR, currentDate+"read_big_csv.log")
logger.add(LOG_FILE,rotation = "2000KB" , encoding='gb2312')   #压缩成zip格式
import glob
def deldir(location):
    # Importing os and glob modules
    # Loop Through the folder projects all files and deleting them one by one
    for file in glob.glob(location+"/*"):
        os.remove(file)
        print("Deleted " + str(file))
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import configparser
class Config:
    def __init__(self,filename,encoding="gbk"):
        logger.info("Config:def __init__(self):初始化"+filename+encoding)
        self.cf = configparser.ConfigParser()
        self.cf.read(filename, encoding=encoding)  # 读取配置文件，如果写文件的绝对路径，就可以不用os模块
    def get_config(self,rootstr,txt):
        return self.cf.get(rootstr, txt)

@logger.catch
def show_title(title,funcxxx, **kwargs):
    logger.info('show_title')

    starttime = datetime.datetime.now()

    funcxxx(title,**kwargs)

    endtime = datetime.datetime.now()
    logger.success('程序运行时间:'+ str(endtime - starttime))

@logger.catch
def set_running(title):
    logger.info('set_running')
    cf = Config("config.ini")
    currentDate = time.strftime("%Y%m%d", time.localtime())
    currentDate = currentDate + "_final"
    targetDir = cf.get_config("IOFileConfig", "exportdir")
    logger.success(targetDir)
    dir_list=os.listdir(targetDir)
    dirfilecount=len(dir_list)
    # logger.success(str(type(dir_list))+str(dirfilecount))
    # pbar = tqdm(dir_list,colour='yellow')
    # for char in pbar:
    #     pbar.set_description("Progress %s" %char)

    scale=dirfilecount
    # logger.success(targetDir)
    # print(scale)
    LL=40
    color_para=Fore.RED
    i=0
    df_sum=0
    # deldir("E:\\tdx_export\\hdf\\")
    # store = pd.HDFStore("E:\\tdx_export\\hdf\\bigdata.h5", mode='w')
    for __list in dir_list:
        i += 1
        df=pd.read_csv(targetDir +__list)
        # logger.success(targetDir +__list
        df.to_hdf("E:\\tdx_export\\hdf\\bigdata.h5", 'df'+str(i),  mode='a')
        # df.to_csv("E:\\tdx_export\\hdf\\" + __list)
        msg = ""
        # store.append('df',df)
        df_size =df.shape[0]
        df_sum+=df_size
        a = (i / scale) * 100
        b = '.' * LL
        c = '▋' * (int((i * LL) / scale))
        b = '.' * ((LL - 1) - int((i * LL) / scale))
        dur = time.perf_counter()  # //每次获取当前时间
        msg1 = ""
        msg2 = ""
        # print(__list)
        print(Style.BRIGHT + Fore.RED + "\r[{}]{:^3.0f}% {}{} {:.2f}s".format(i, a, c, b, dur), df_size,df_sum, end=' ')
        # time.sleep(0.01)
        # if i == 10:
        #     break
    print("循环", i,"DF sum",df_sum)
    # print(char)
if __name__ == '__main__':
    os.system("cls")
    show_title("title",funcxxx=set_running)
